<?php
//---------------------------------
// Autho: King
// Date: 2011-12-30 13:28:48
//---------------------------------
class ClassXML
{
	private $dom;
	private $root;
	
	public function __construct($total)
	{
		$this->dom = new DOMDocument("1.0", "UTF-8"); 
		header("Content-Type: text/plain"); 
		$this->root = $this->dom->createElement("PPSChannels"); 
		$this->dom->appendChild($this->root);
		
		$ClassNum = $this->dom->createAttribute("ClassNum"); 
		$this->root->appendChild($ClassNum); 
		$ClassNumValue = $this->dom->createTextNode($total); 
		$ClassNum->appendChild($ClassNumValue);
	} 
	
	function addClass($ID,$Title,$ContentNum)
	{
		$class = $this->dom->createElement("Class"); 
		$this->root->appendChild($class);
		
		$item = $this->dom->createElement("ID"); 
		$class->appendChild($item);
		$cdata = $this->dom->createCDATASection($ID); 
		$item->appendChild($cdata); 
		
		
		$item = $this->dom->createElement("Title"); 
		$class->appendChild($item);
		$cdata = $this->dom->createCDATASection($Title); 
		$item->appendChild($cdata); 
		
		
		$item = $this->dom->createElement("ContentNum"); 
		$class->appendChild($item);
		$cdata = $this->dom->createCDATASection($ContentNum); 
		$item->appendChild($cdata); 
	} 
	
	function saveClassXML()
	{
		echo $this->dom->saveXML();
	}
}

function file_data($url) 
{
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
	curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
	@ $data = curl_exec($ch);
	curl_close($ch);
	//@ $data = iconv('UTF-8', 'GBK', $data);
	return $data;
}

function get_youkuclass()
{
	$top_url = "http://www.youku.com/v/";
	$str =  file_data($top_url);
	
	$a = preg_match('|<ul class="tree">(.*?)<\/ul>|ims', $str, $tmp);
	if ($a < 1) exit;
	$b = preg_match_all('|">(.*?)<\/a><\/li>|', $tmp[1], $li);
	$c = preg_match_all('|<li><a href="(.*?)" charset="|', $tmp[1], $href);
	
	if ($b == $c)
	{
		$xml = new ClassXML($b);
		for($i = 0; $i < $b;$i++)
		{
			$xml->addClass($href[1][$i],$li[1][$i],"  ");
		}
		$xml->saveClassXML();
	}
}

get_youkuclass();
?>
